from app import app
from flask import Flask, render_template, request, jsonify, make_response, session
from flask_restful import inputs
from app.libs.Codes import TextCode
from app.libs.Verifys import passWord
from app.models import *
from app.admin.admin_login import admin_require
from app.libs.QINIU import delect as QiniuDel, pfop_m3u8
from sqlalchemy import or_
import time
import hashlib
import random
from app.setting import *
from html import unescape
import re
from sqlalchemy.orm.attributes import flag_modified

# 获积分套餐列表
@app.route('/pdd/admin/integarl/meal', methods=["post", "get"])
@admin_require
def admin_integarl_meal(aid):
    if not aid.job.price:
        return jsonify(code=-103,msg="无权访问,需要管理员授权")

    meal = IntegralMeal.query.all()
    if not meal:
        return jsonify(code=-104,msg="没找到该条记录,404页面丢失")

    data = [{
        "id":i.id,
        "name":i.name,
        "price":i.price,
        "integral":i.integral,
        "give":i.give,
        "rateFlag":i.rateFlag,
        "rate":i.rate,
        "rateName":i.rateName,
        "updateTime":str(i.updateTime)
    } for i in meal]

    return jsonify(code=0,msg="获取成功",data=data)

# 修改积分套餐
@app.route('/pdd/admin/integarl/meal/edit', methods=["post", "get"])
@admin_require
def admin_integarl_meal_edit(aid):
    if not aid.job.price:
        return jsonify(code=-103, msg="没有权限")

    id = request.form.get("id",type=int)
    integral = request.form.get("integral", type=int)
    give = request.form.get("give", type=int)
    price = request.form.get("price", type=float)
    name = request.form.get("name", type=str)
    rateFlag = request.form.get("rateFlag", type=inputs.boolean)
    rate = request.form.get("rate", type=float)
    rateName = request.form.get("rateName", type=str)
    print(give)
    if id is None:
        return jsonify(code=-1, msg="套餐id传入错误")

    if rateFlag:
        if rate is None:
            return jsonify(code=-1, meg="折扣开启后请填写折扣")
        if rateName is None:
            return jsonify(code=-1, meg="折扣开启后请填写折扣名称")


    meal = IntegralMeal.query.get(id)
    if not meal:
        return jsonify(code=-1, msg="没有找到该条记录，请刷新重试～")
    operate_comment =[]
    if integral is not None and integral != meal.integral:
        operate_comment.append({"name": "充值积分", "raw": meal.integral, "content": integral})
        meal.integral = integral
    if give is not None and give != meal.give:
        operate_comment.append({"name": "赠送积分", "raw": meal.give, "content": give})
        meal.give = give

    if price is not None and price != meal.price:
        operate_comment.append({"name": "使用价格", "raw": meal.price, "content": price})
        meal.price = price

    if name is not None and name != meal.name:
        operate_comment.append({"name": "套餐名称", "raw": meal.name, "content": name})
        meal.name = name

    if rateFlag is not None:
        operate_comment.append({"name": "折扣状态", "raw": meal.rateFlag, "content": rateFlag})
        meal.rateFlag = rateFlag
        if meal.rateFlag:
            if rate is not None and rate != meal.rate:

                operate_comment.append({"name": "折扣", "raw": meal.rate, "content": rate})
                meal.rate = rate
            if rateName is not None and rateName != meal.rateName:
                operate_comment.append({"name": "折扣名称", "raw": meal.rateName, "content": rateName})
                meal.rateName = rateName
        else:
            operate_comment.append({"name": "折扣", "raw": meal.rate, "content": None})
            meal.rate = None
            operate_comment.append({"name": "折扣名称", "raw": meal.rateName, "content": None})
            meal.rateName = None



    if len(operate_comment) > 0:
        log = Admin_operate_log(func=meal.name, up=2, comment=operate_comment, aid=aid.id)
        db.session.add(log)

    try:
        db.session.commit()
        return jsonify(code=0, msg="修改成功", data={
        "id":meal.id,
        "name":meal.name,
        "price":meal.price,
        "integral":meal.integral,
        "give":meal.give,
        "rateFlag":meal.rateFlag,
        "rate":meal.rate,
        "rateName":meal.rateName,
        "updateTime":str(meal.updateTime)
    })
    except:
        db.session.rollback()
        return jsonify(code=-1, msg="修改失败")
